package com.companyName.generator;

import com.companyName.consolelog.LabelLogAppender;
import com.companyName.consolelog.TextAreaLogAppender;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.*;

public class CodeGenWin extends JFrame{

	private static final long serialVersionUID = 1L;

	private final static Log log = LogFactory.getLog(CodeGenWin.class);

	private JToolBar toolBar = new JToolBar();
	private JButton btnGenerate = new JButton("生成");
	
	private JPanel pnlNorth = new JPanel();
	private JPanel pnlSettings = new JPanel();
	private JTabbedPane tpnlDbTables = new JTabbedPane();
	
	private JTable tblDbTables = new JTable(new String[][]{}, new String[]{"选择", "表名", ""});
	private JScrollPane spnlDbTables = new JScrollPane(tblDbTables);  
	
	private JTextArea txtaConsole = new JTextArea();
	private JScrollPane spnlConsole = new JScrollPane(txtaConsole);


	private JLabel lblSampleTitle = new JLabel("项目框架自动生成器");
	private JLabel lblSampleDir = new JLabel("样板项目路径");
	private JTextField txtSampleDir = new JTextField("D:\\project\\generator\\fw-sample");
	private JButton btnSampleDir = new JButton("...");
	private JLabel lblTargetDir = new JLabel("框架存放路径");
	private JTextField txtTargetDir = new JTextField("D:\\project\\generator\\sports");
	private JButton btnTargetDir = new JButton("...");
	private JLabel lblDbType = new JLabel("数据库类型");
	private JComboBox<String> cbxDbType = new JComboBox<String>();
	private JLabel lblUserName = new JLabel("用户名");
	private JTextField txtUserName = new JTextField("root");
	private JLabel lblPassword = new JLabel("密码");
	private JTextField txtPassword = new JTextField("root");
	private JLabel lblDbUrl = new JLabel("数据库URL");
	private JTextField txtDbUrl = new JTextField("jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=UTF-8");
	private JButton btnGetTableList = new JButton(">");

	private JLabel lblCompanyName = new JLabel("公司名称");
	private JTextField txtCompanyName = new JTextField("companyName");
	private JLabel lblProjectName = new JLabel("项目名称");
	private JTextField txtProjectName = new JTextField("sports");

	private JLabel lblPluginsName = new JLabel("插件名称");
	private JLabel lblPluginsExp = new JLabel("cat;hessian;dubbo;rabbitmq");
	private JTextField txtPluginsName = new JTextField("");
//	private JTextField txtProjectName = new JTextField("\\$\\{project_name\\}");

	public CodeGenWin() {
		initWindow();
		addComponents();
		addListeners();
		setVisible(true);
	}

	private void initWindow() {
		setSize(800,600);  
		setLocationRelativeTo(null);  
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		Toolkit kit=Toolkit.getDefaultToolkit();
        Dimension screenSize=kit.getScreenSize(); 
        int width=screenSize.width;
        int height=screenSize.height;
        int x=(width-getWidth())/2;
        int y=(height-getHeight())/2;
        setLocation(x, y);
        
		cbxDbType.setEditable(false);
		cbxDbType.addItem("mysql");
	    cbxDbType.addItem("oracle");

		toolBar.add(btnGenerate);

		tpnlDbTables.addTab("控制台", spnlConsole);
	    tpnlDbTables.addTab("表列表", spnlDbTables);

		pnlNorth.setLayout(new BorderLayout());

		tblDbTables.setFillsViewportHeight(true);

		initLog();
	/*	txtaConsole.setColumns(20);
		txtaConsole.setRows(5);
		spnlConsole.setViewportView(txtaConsole);*/
	}

	public void initLog() {
		try {
			Thread  t2;
			t2 = new TextAreaLogAppender(txtaConsole, spnlConsole);
//            t1.start();
			t2.start();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e, "绑定日志输出组件错误", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 * 赋值给生成代码调用
	 */
	private void addListeners() {
		btnSampleDir.addActionListener(new SelectDirListener(txtSampleDir));
		btnTargetDir.addActionListener(new SelectDirListener(txtTargetDir));
		GenerateCodeListener gcl = new GenerateCodeListener();
		gcl.setTxtSampleDir(txtSampleDir);
		gcl.setTxtTargetDir(txtTargetDir);
		gcl.setTxtDbUrl(txtDbUrl);
		gcl.setTxtUserName(txtUserName);
		gcl.setTxtPassword(txtPassword);
		gcl.setTxtCompanyName(txtCompanyName);
		gcl.setTxtProjectName(txtProjectName);
		gcl.setTxtPluginsName(txtPluginsName);
		btnGenerate.addActionListener(gcl);

	}

	private void addComponents() {
		getContentPane().add(pnlNorth, BorderLayout.NORTH);
		pnlNorth.add(toolBar, BorderLayout.NORTH);
		pnlNorth.add(pnlSettings, BorderLayout.CENTER);
		
		getContentPane().add(tpnlDbTables, BorderLayout.CENTER);
		
		GridBagLayout layout = new GridBagLayout();
		pnlSettings.setLayout(layout);
		//x,y,width,height,weightx,weighty,anchor,fill,insets,padx,pady
		/*layout.setConstraints(lblSampleDir, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(lblSampleDir);
		layout.setConstraints(txtSampleDir, new GridBagConstraints(1, 0, 5, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(txtSampleDir);
		layout.setConstraints(btnSampleDir, new GridBagConstraints(6, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(btnSampleDir);*/

		layout.setConstraints(lblSampleTitle, new GridBagConstraints(0, 0, 7, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(lblSampleTitle);
		
		layout.setConstraints(lblTargetDir, new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(lblTargetDir);
		layout.setConstraints(txtTargetDir, new GridBagConstraints(1, 1, 5, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(txtTargetDir);
		layout.setConstraints(btnTargetDir, new GridBagConstraints(6, 1, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(btnTargetDir);
		
		layout.setConstraints(lblDbType, new GridBagConstraints(0, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(lblDbType);
		layout.setConstraints(cbxDbType, new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(cbxDbType);
		layout.setConstraints(lblUserName, new GridBagConstraints(2, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(lblUserName);
		layout.setConstraints(txtUserName, new GridBagConstraints(3, 2, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(txtUserName);
		layout.setConstraints(lblPassword, new GridBagConstraints(4, 2, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(lblPassword);
		layout.setConstraints(txtPassword, new GridBagConstraints(5, 2, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(txtPassword);
		
		layout.setConstraints(lblDbUrl, new GridBagConstraints(0, 3, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(lblDbUrl);
		layout.setConstraints(txtDbUrl, new GridBagConstraints(1, 3, 5, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(txtDbUrl);
		layout.setConstraints(btnGetTableList, new GridBagConstraints(6, 3, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(1, 1, 1, 1), 0, 0));  
		pnlSettings.add(btnGetTableList);
		
		layout.setConstraints(lblCompanyName, new GridBagConstraints(0, 4, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(lblCompanyName);
		layout.setConstraints(txtCompanyName, new GridBagConstraints(1, 4, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(txtCompanyName);

		layout.setConstraints(lblProjectName, new GridBagConstraints(0, 5, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(lblProjectName);
		layout.setConstraints(txtProjectName, new GridBagConstraints(1, 5, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(txtProjectName);

		layout.setConstraints(lblPluginsName, new GridBagConstraints(0, 6, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(lblPluginsName);
		layout.setConstraints(lblPluginsExp, new GridBagConstraints(1, 6, 1, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(lblPluginsExp);
		layout.setConstraints(txtPluginsName, new GridBagConstraints(2, 6, 4, 1, 1, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(1, 1, 1, 1), 0, 0));
		pnlSettings.add(txtPluginsName);



		
		for(Component c : pnlSettings.getComponents()){
			if(c instanceof JLabel){
				c.setPreferredSize(new Dimension(100,25));
			}
		}
		
	}


}
